package common

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"testPaper/model"
)

var DB *gorm.DB

func IntDB() *gorm.DB {
	driverName := "mysql"
	//host := "47.96.239.34"

	host := "121.40.135.100"
	database := "testpaper"
	username := "testPaper"
	port := "3306"
	password := "Wukeli.1"

	// 本地
	//host := "localhost"
	//database := "testpaper"
	//username := "root"
	//password := "123456"
	//port := "3306"

	charset := "utf8"

	args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true",
		username,
		password,
		host,
		port,
		database,
		charset)
	db, err := gorm.Open(driverName, args)
	if err != nil {
		fmt.Println("连接失败：" + err.Error())
	}
	fmt.Println("连接成功！！！")
	db.AutoMigrate(
		&model.Admin{},
		&model.TestPaper{},
		&model.Topic{},
		&model.Company{},
		&model.WxUser{},
		&model.TestPaperSort{},
		&model.TestPaperTag{},
		&model.Banner{},
		&model.NewTestPaper{},
		&model.Case{},
		&model.NewTopic{})
	DB = db
	return db
}
func GetDB() *gorm.DB {
	return DB
}
